package com.mingyizhudao.app.server;

import java.util.Map;

import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.config.SystemConfig;
import com.dixiang.framework.config.SystemConstants;
import com.google.gson.reflect.TypeToken;
import com.mingyizhudao.app.common.IDataLoadListenterByFlg;
import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class DataCheckService extends Service{
	private Context mContext;
	private int sum = 0;
	private Object dataVersion;
	
	@Override
	public void onCreate() {
		super.onCreate();
		mContext = this;
		SystemConstants.putServerKey(SystemConfig.SERVER_KEY);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SystemConstants.removeServerKey(SystemConfig.SERVER_KEY);
		Log.e("===", "server onDestroy");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		getCheckData();
		Log.e("===", "server start");
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	private void getCheckData(){
		DataUtils.getCheckData(mContext, loadListenter , Constants.HTTP_INDEX.First , new TypeToken<QjResult<Map<String,Object>>>(){});
	}
	
	private void getLocalData(){
		DataUtils.getLocaldataFromNet(mContext, loadListenter , Constants.HTTP_INDEX.Second , new TypeToken<QjResult<Map<String,Map<String,Object>>>>(){});
	}

	private IDataLoadListenterByFlg loadListenter = new IDataLoadListenterByFlg() {

		@Override
		public void onSuccess(QjResult result, String flg) {
			super.onSuccess(result, flg);
			if(flg.equals(Constants.HTTP_INDEX.First)){
				Map map = (Map<String,Object>) result.getResults();
				if(map != null && !map.isEmpty()){
					String localUrl = map.get("localdataUrl") + "";
					CommonUtils.setLocaldataFromNetUrl(mContext,localUrl);
					dataVersion = map.get("version");
					int version = dataVersion == null ? 0 : Integer.parseInt(dataVersion.toString());
					int currentDataVersion = Integer.parseInt(CommonUtils.getDataVersion(mContext));
					
					if (version > currentDataVersion) {
						getLocalData();
					}
				}else{
					stopSelf();
				}	
			}else{
				Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) result.getResults();
				if(map != null && !map.isEmpty()){
					Map<String, Object> localMap = map.get("url");
					if(localMap != null){
						CommonUtils.setDataVersion(mContext, "" + dataVersion);
						CommonUtils.setSendCodeUrl(mContext,
								localMap.get("sendCodeUrl")+"");
						CommonUtils
								.setLoginUrl(mContext, localMap.get("loginUrl")+"");
						CommonUtils.setBookingListUrl(mContext,
								localMap.get("bookingListUrl")+"");
						CommonUtils.setDoctoUrl(mContext, localMap.get("doctorUrl")+"");
						CommonUtils.setBookingUrl(mContext, localMap.get("bookingUrl")+"");
						CommonUtils.setBookingfileUrl(mContext, localMap.get("bookingfileUrl")+"");
						CommonUtils.setPaymentUrl(mContext, localMap.get("paymentUrl")+"");
						CommonUtils.setHospitalUrl(mContext, localMap.get("hospitalUrl")+"");
					}
				}
				stopSelf();
			}
			
		}

		@Override
		public void onError(Exception ex, Object result, String flg) {
			super.onError(ex, result, flg);
			stopSelf();
		}

		@Override
		public void onFailure(QjResult result, String flg) {
			super.onFailure(result, flg);
			Log.e("=====", result.getErrorMsg());
			stopSelf();
		}

		@Override
		public void onCompleted(Exception e, String flg) {
			super.onCompleted(e, flg);
		}

		@Override
		public void onHttpError(int msg , String flg) {
			super.onHttpError(msg);
			onMaxFailure(flg);
			Log.e("=====", "http error "+msg);
		}
		
	};
	
	private void onMaxFailure(String flg){
		sum++;
		if (sum <= 5 && flg.equals(Constants.HTTP_INDEX.First)) {
			getCheckData();
		}else if(sum <= 5 && flg.equals(Constants.HTTP_INDEX.Second)){
			getLocalData();
		}else{
			stopSelf();
		}
	}
}
